% 指定文件路径
filename = 'E:\缝纫机波形识别\DGIOT.csv';

% 读取文件
data = readtable(filename);
column2 = data{:, 5};

% 我们要抽取的数据的数量
numSamples = 200;

% 我们要抽取的数据组的数量
numGroups = 10000;

% 初始化一个矩阵来存储抽取的数据
% 运行数据
extractedData = zeros(numGroups, numSamples);

% 抽取数据
for i = 1:numGroups
    % 生成一个随机索引
    index = randi([1, length(column2) - numSamples + 1]);
    
    % 抽取数据
    extractedData(i, :) = column2(index:index+numSamples-1);
end

column3 = data{:, 6};

% 初始化一个矩阵来存储抽取的数据
% 暂停数据
extractedData2 = zeros(numGroups, numSamples);

% 抽取数据
for i = 1:numGroups
    % 生成一个随机索引
    index = randi([1, length(column3) - numSamples + 1]);
    
    % 抽取数据
    extractedData2(i, :) = column3(index:index+numSamples-1);
end
column33 = data{:, 8};
extractedData22 = zeros(numGroups, numSamples);

% 抽取数据
for i = 1:numGroups
    % 生成一个随机索引
    index = randi([1, length(column33) - numSamples + 1]);
    
    % 抽取数据
    extractedData22(i, :) = column33(index:index+numSamples-1);
end
extractedData2=[extractedData2;extractedData22];
perm = randperm(20000);
% 选择排列的前10000个元素，这将给你一个随机的10000行的子集
extractedData2 = extractedData2(perm(1:10000), :);


column4 = data{600000:1100000, 7};

% 初始化一个矩阵来存储抽取的数据
% 关机数据
extractedData3 = zeros(numGroups, numSamples);

% 抽取数据
for i = 1:numGroups
    % 生成一个随机索引
    index = randi([1, length(column4) - numSamples + 1]);
    
    % 抽取数据
    extractedData3(i, :) = column4(index:index+numSamples-1);
end

Xtrainorigin=[extractedData;extractedData2;extractedData3];
XTrain = cell(1, size(Xtrainorigin, 1));
for i = 1:size(Xtrainorigin, 1)
    XTrain{i} = Xtrainorigin(i, :)';
end
% 创建标签向量
labels = [3*ones(10000, 1); 2*ones(10000, 1); 1*ones(10000, 1)];
YTrain = categorical(labels);


numFeatures = 200;
numHiddenUnits = 1000;
numClasses = 3;

layers = [ ...
    sequenceInputLayer(numFeatures)
    lstmLayer(numHiddenUnits,'OutputMode','last')
    fullyConnectedLayer(numClasses)
    softmaxLayer
    classificationLayer];

options = trainingOptions('adam', ...
    'MaxEpochs',30, ...
    'GradientThreshold',1, ...
    'InitialLearnRate',0.005, ...
    'LearnRateSchedule','piecewise', ...
    'LearnRateDropPeriod',20, ...
    'Verbose',0, ...
    'Plots','training-progress');

net = trainNetwork(XTrain,YTrain,layers,options);

testData = readmatrix('test.xlsx', 'Range', 'A1:GR5');

% 将数据转换为适合模型输入的格式
% 假设你的模型接受1xN的cell数组作为输入，每个cell包含一个200x1的序列
XTest = num2cell(testData, 2); % 将每一行转换为一个cell
XTest = cellfun(@(x) reshape(x, [], 1), XTest, 'UniformOutput', false); % 将每个cell内的数据转换为200x1的序列

% 使用模型进行预测
YPred = classify(net, XTest);

% 打印预测结果
disp(YPred);